package com.top.ruizhong.popup

import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.view.animation.TranslateAnimation
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ScreenUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.top.ruizhong.R
import razerdp.basepopup.BasePopupWindow


class VideoDiscussPopup(
    context: Context, val onConfirm: () -> Unit
) : BasePopupWindow(context) {

    init {
        setAdjustInputMethod(true)
        height = ScreenUtils.getAppScreenHeight() / 4 * 3
        popupGravity = Gravity.BOTTOM
        setAutoShowInputMethod(false)
        setOverlayNavigationBar(false)
    }

    private val discuss2Adapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_video_discuss_2) {
            override fun convert(holder: BaseViewHolder, item: String) {
            }
        }

    private val discussAdapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_video_discuss) {

            override fun convert(holder: BaseViewHolder, item: String) {
                holder.getView<RecyclerView>(R.id.rvVideoDiscussBody).adapter =
                    discuss2Adapter.apply {
                        val discuss: MutableList<String> = mutableListOf()
                        for (index: Int in 0 until 1) {
                            discuss.add(index.toString())
                        }
                        setNewInstance(discuss)
                    }
            }
        }

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.popup_video_discuss)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        contentView.findViewById<ImageView>(R.id.ivVideoDiscussClose).setOnClickListener {
            dismiss()
        }
    }

    override fun onCreateShowAnimation(width: Int, height: Int): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f,
            Animation.RELATIVE_TO_PARENT, 0f
        ).apply { duration = 400L }
    }

    override fun onCreateDismissAnimation(): Animation {
        return TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 0f,
            Animation.RELATIVE_TO_PARENT, 1f
        ).apply { duration = 400L }
    }

    fun setFillData(): VideoDiscussPopup {
        contentView.findViewById<RecyclerView>(R.id.rvVideoDiscussBody).adapter =
            discussAdapter.apply {
                val discuss: MutableList<String> = mutableListOf()
                for (index: Int in 0..5) {
                    discuss.add(index.toString())
                }
                setNewInstance(discuss)
            }
        return this
    }

}